package com.betterjr.modules.risk.loan.entity;

import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

import com.betterjr.common.annotation.MetaData;
import com.betterjr.common.entity.BetterjrEntity;
import com.betterjr.common.selectkey.SerialGenerator;
import com.betterjr.modules.contract.entity.ContractTemplate;
import com.betterjr.modules.risk.loan.data.LoanProjectElcAgreementConstantCollections;
import com.betterjr.modules.risk.loan.data.LoanTransactionRelationConstantCollections;

@Access(AccessType.FIELD)
@Entity
@Table(name = "t_scf_loanproject_elcagreement")
public class LoanProjectElcAgreement implements BetterjrEntity {

    /** 
     * @Fields serialVersionUID : 
     */
    private static final long serialVersionUID = -4839749928453538809L;

    /**
     * 流水号
     */
    @Id
    @Column(name = "ID", columnDefinition = "INTEGER")
    @MetaData(value = "流水号", comments = "流水号")
    private Long id;

    /**
     * 贷款项目id
     */
    @Column(name = "L_LOAN_ID", columnDefinition = "INTEGER")
    @MetaData(value = "贷款项目id", comments = "贷款项目id")
    private Long loanId;

    /**
     * 1:账户监管 2 平台服务协议  3确权协议 4 其他协议
     */
    @Column(name = "C_TYPE", columnDefinition = "VARCHAR")
    @MetaData(value = "1:账户监管 2 平台服务协议  3确权协议 4 其他协议", comments = "1:账户监管 2 平台服务协议  3确权协议 4 其他协议")
    private String type;

    /**
     * 1 线上签署   2 线下签署
     */
    @Column(name = "C_SUBTYPE", columnDefinition = "VARCHAR")
    @MetaData(value = "1 线上签署   2 线下签署", comments = "1 线上签署   2 线下签署")
    private String subType;

    /**
     * 合同名称
     */
    @Column(name = "C_AGGREMENT_NAME", columnDefinition = "VARCHAR")
    @MetaData(value = "合同名称", comments = "合同名称")
    private String aggrementName;

    /**
     * 上传批次号
     */
    @Column(name = "L_BATCHNO", columnDefinition = "INTEGER")
    @MetaData(value = "上传批次号", comments = "上传批次号")
    private Long batchNo;

    /**
     * 0 失效  1 生效
     */
    @Column(name = "C_BUSINSTATUS", columnDefinition = "VARCHAR")
    @MetaData(value = "0 失效  1 生效", comments = "0 失效  1 生效")
    private String businStatus;

    /**
     * 上传文件id
     */
    @Column(name = "L_FILEID", columnDefinition = "INTEGER")
    @MetaData(value = "上传文件id", comments = "上传文件id")
    private Long fileId;

    /**
     * 附件文件名称/模版文件名称
     */
    @Column(name = "C_FILENAME", columnDefinition = "VARCHAR")
    @MetaData(value = "附件文件名称", comments = "附件文件名称")
    private String fileName;

    /**
     * 合同模版id
     */
    @Column(name = "L_CONTRACT_TEMPLATEID", columnDefinition = "INTEGER")
    @MetaData(value = "合同模版id", comments = "合同模版id")
    private Long contractTemplateId;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getLoanId() {
        return loanId;
    }

    public void setLoanId(Long loanId) {
        this.loanId = loanId;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getSubType() {
        return subType;
    }

    public void setSubType(String subType) {
        this.subType = subType;
    }

    public String getAggrementName() {
        return aggrementName;
    }

    public void setAggrementName(String aggrementName) {
        this.aggrementName = aggrementName;
    }

    public Long getBatchNo() {
        return batchNo;
    }

    public void setBatchNo(Long batchNo) {
        this.batchNo = batchNo;
    }

    public String getBusinStatus() {
        return businStatus;
    }

    public void setBusinStatus(String businStatus) {
        this.businStatus = businStatus;
    }

    public Long getFileId() {
        return fileId;
    }

    public void setFileId(Long fileId) {
        this.fileId = fileId;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public Long getContractTemplateId() {
        return contractTemplateId;
    }

    public void setContractTemplateId(Long contractTemplateId) {
        this.contractTemplateId = contractTemplateId;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((aggrementName == null) ? 0 : aggrementName.hashCode());
        result = prime * result + ((batchNo == null) ? 0 : batchNo.hashCode());
        result = prime * result + ((businStatus == null) ? 0 : businStatus.hashCode());
        result = prime * result + ((contractTemplateId == null) ? 0 : contractTemplateId.hashCode());
        result = prime * result + ((fileId == null) ? 0 : fileId.hashCode());
        result = prime * result + ((fileName == null) ? 0 : fileName.hashCode());
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        result = prime * result + ((loanId == null) ? 0 : loanId.hashCode());
        result = prime * result + ((type == null) ? 0 : type.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (!(obj instanceof LoanProjectElcAgreement)) {
            return false;
        }
        LoanProjectElcAgreement other = (LoanProjectElcAgreement) obj;
        if (aggrementName == null) {
            if (other.aggrementName != null) {
                return false;
            }
        } else if (!aggrementName.equals(other.aggrementName)) {
            return false;
        }
        if (batchNo == null) {
            if (other.batchNo != null) {
                return false;
            }
        } else if (!batchNo.equals(other.batchNo)) {
            return false;
        }
        if (businStatus == null) {
            if (other.businStatus != null) {
                return false;
            }
        } else if (!businStatus.equals(other.businStatus)) {
            return false;
        }
        if (contractTemplateId == null) {
            if (other.contractTemplateId != null) {
                return false;
            }
        } else if (!contractTemplateId.equals(other.contractTemplateId)) {
            return false;
        }
        if (fileId == null) {
            if (other.fileId != null) {
                return false;
            }
        } else if (!fileId.equals(other.fileId)) {
            return false;
        }
        if (fileName == null) {
            if (other.fileName != null) {
                return false;
            }
        } else if (!fileName.equals(other.fileName)) {
            return false;
        }
        if (id == null) {
            if (other.id != null) {
                return false;
            }
        } else if (!id.equals(other.id)) {
            return false;
        }
        if (loanId == null) {
            if (other.loanId != null) {
                return false;
            }
        } else if (!loanId.equals(other.loanId)) {
            return false;
        }
        if (type == null) {
            if (other.type != null) {
                return false;
            }
        } else if (!type.equals(other.type)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "LoanProjectElcAgreement [id=" + id + ", loanId=" + loanId + ", type=" + type + ", subType=" + subType
                + ", aggrementName=" + aggrementName + ", batchNo=" + batchNo + ", businStatus=" + businStatus
                + ", fileId=" + fileId + ", fileName=" + fileName + ", contractTemplateId=" + contractTemplateId + "]";
    }

    public static LoanProjectElcAgreement toAgreementByTemplate(ContractTemplate template, String anBusinTypeCode,
            Long anLoanId) {

        LoanProjectElcAgreement agreement = new LoanProjectElcAgreement();

        agreement.saveAddInitValue();

        agreement.setAggrementName(template.getName());
        agreement.setBatchNo(template.getTemplate());
        agreement.setContractTemplateId(template.getId());
        agreement.setFileId(template.getTemplateId());
        agreement.setFileName(template.getTemplateName());
        agreement.setType(convertBusinTypeToType(anBusinTypeCode));
        agreement.setSubType(LoanProjectElcAgreementConstantCollections.ELCAGREEMENT_SUBTYPE_UNDERLINE);
        agreement.setLoanId(anLoanId);
        return agreement;
    }

    public void saveAddInitValue() {

        this.setBusinStatus(LoanTransactionRelationConstantCollections.RELATIONS_BUSINSTATUS_OK);

        this.setId(SerialGenerator.getLongValue("LoanProjectElcAgreement.id"));

    }

    public static String convertBusinTypeToType(String anBusinType) {

        return LoanProjectElcAgreementConstantCollections.ELCAGREEMENT_BUSINTYPE_ACCOUNTREGULATORY.equals(anBusinType)
                ? LoanProjectElcAgreementConstantCollections.ELCAGREEMENT_TYPE_ACCOUNTREGULATORY
                : LoanProjectElcAgreementConstantCollections.ELCAGREEMENT_BUSINTYPE_DATACONFIGM.equals(anBusinType)
                        ? LoanProjectElcAgreementConstantCollections.ELCAGREEMENT_TYPE_DATACONFIGM
                        : LoanProjectElcAgreementConstantCollections.ELCAGREEMENT_BUSINTYPE_PLATFORMSERVICE
                                .equals(anBusinType)
                                        ? LoanProjectElcAgreementConstantCollections.ELCAGREEMENT_TYPE_PLATFORMSERVICE
                                        : LoanProjectElcAgreementConstantCollections.ELCAGREEMENT_TYPE_OTHERAGREEMENTS;

    }

    public static String convertTypeToBusinType(String anType) {

        return LoanProjectElcAgreementConstantCollections.ELCAGREEMENT_TYPE_ACCOUNTREGULATORY.equals(anType)
                ? LoanProjectElcAgreementConstantCollections.ELCAGREEMENT_BUSINTYPE_ACCOUNTREGULATORY
                : LoanProjectElcAgreementConstantCollections.ELCAGREEMENT_TYPE_DATACONFIGM.equals(anType)
                        ? LoanProjectElcAgreementConstantCollections.ELCAGREEMENT_BUSINTYPE_DATACONFIGM
                        : LoanProjectElcAgreementConstantCollections.ELCAGREEMENT_TYPE_PLATFORMSERVICE.equals(anType)
                                ? LoanProjectElcAgreementConstantCollections.ELCAGREEMENT_BUSINTYPE_PLATFORMSERVICE
                                : LoanProjectElcAgreementConstantCollections.ELCAGREEMENT_BUSINTYPE_OTHERAGREEMENTS;

    }

}
